{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3 µs ± 19.2 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n",
      "[37.2, 37.6, 36.8]\n"
     ]
    }
   ],
   "source": [
    "## 使用循环计算采购总额\n",
    "def veg_add(price,num):\n",
    "    total = []\n",
    "    for row in price:\n",
    "        Sum = 0\n",
    "        for i,j in zip(row,num):\n",
    "            Sum += i * j\n",
    "        total.append(round(Sum,1))\n",
    "    return total\n",
    "\n",
    "price = [[1.2,1.5,1.8],[1.3,1.4,1.9],[1.1,1.6,1.7]]\n",
    "num = [5,10,9]\n",
    "%timeit total = veg_add(price,num)\n",
    "print(total)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.23 µs ± 4.12 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n",
      "[37.2 37.6 36.8]\n"
     ]
    }
   ],
   "source": [
    "## 使用矩阵点乘计算采购总额\n",
    "import numpy as np\n",
    "def veg_dot_add(price2,num2):\n",
    "    total2 = np.dot(price2,num2)\n",
    "    return total2\n",
    "\n",
    "price2 = np.array([[1.2,1.5,1.8],[1.3,1.4,1.9],[1.1,1.6,1.7]])\n",
    "num2 = np.array([5,10,9])\n",
    "%timeit total2 = veg_dot_add(price2,num2)\n",
    "print(total2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
